<?php

namespace App\Http\Controllers\Api;

use App\ForumComment;
use App\ForumPost;
use App\Http\Requests\CommentStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ForumCommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(CommentStore $request)
    {
        $res = ForumComment::create(array_merge($request->only(['post_id', 'target_id', 'comment']), ['user_id' => Auth::id()]));
        if ($res) {
            ForumPost::where('id',$request->post_id)->increment('comment_num');
            return response(['message' => '评论成功']);
        }
        return response(['message' => '评论失败'], 400);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\ForumComment $forumComment
     * @return \Illuminate\Http\Response
     */
    public function show(ForumComment $forumComment)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ForumComment $forumComment
     * @return \Illuminate\Http\Response
     */
    public function edit(ForumComment $forumComment)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\ForumComment $forumComment
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ForumComment $forumComment)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\ForumComment $forumComment
     * @return \Illuminate\Http\Response
     */
    public function destroy(ForumComment $forumComment)
    {
        $forumComment->delete();
        ForumPost::where('id',$forumComment->post_id)->decrement('comment_num');
        return response(['message' => '删除成功']);
    }
}
